'''
# 提取医生的三分类预测结果
# 提取医生的五分类预测结果
# 计算医生在三分类上的预测精度（recall, precision）
# 计算医生在五分类上的预测精度（recall, precision）
'''

# 将医生的猜测结果转换为三分类结果
import pandas as pd
from sklearn.metrics import classification_report, confusion_matrix

ori_path = 'D:/lung_cancer/data/doctor_result.csv'
labels = 'D:/lung_cancer/data/label.csv'

# 三分类结果转换
def three_class_resave():
    data = pd.read_csv(ori_path)
    print(len(data))

    data2 = pd.read_csv(labels)

    lines = []
    for i in range(len(data)):
        patientid = data['patientID'][i]
        doctor_pred = int(data['type'][i])
        five_label = -1

        for j in range(len(data2)):
            if str(patientid) == str(data2['patient_id'][j]):
                five_label = int(data2['cancer_type'][j])

        if five_label == 0:
            five_label = 5

        three_label = five_label
        three_pred = doctor_pred

        if five_label == 3 or five_label == 4 or five_label == 5:
            three_label = 3

        if doctor_pred == 3 or doctor_pred == 4 or doctor_pred == 5:
            three_pred = 3

        line = [patientid, five_label, doctor_pred, three_label, three_pred]
        lines.append(line)

    df = pd.DataFrame(lines, columns=['patientid', 'five_label', 'five_pred', 'three_label', 'three_pred'])
    df.to_csv('D:/lung_cancer/data/doctor_multi_result.csv', index=False)


# 对医生三分类结果统计精度
def statistic_three_classfication():
    data = pd.read_csv('D:/lung_cancer/data/doctor_multi_result.csv')
    three_label = list(data['three_label'])
    three_pred = list(data['three_pred'])
    # print(list(three_label))
    # print(list(three_pred))

    # 精度评估
    target_names = ['class 1', 'class 2', 'class 3']
    print(classification_report(three_label, three_pred, target_names=target_names))

    # 计算混淆矩阵
    confusion = confusion_matrix(three_label, three_pred)
    print(confusion)


# 对医生五分类结果统计精度
def statistic_five_classfication():
    data = pd.read_csv('D:/lung_cancer/data/doctor_multi_result.csv')
    five_label = list(data['five_label'])
    five_pred = list(data['five_pred'])
    # print(list(five_label))
    # print(list(five_pred))

    # 计算评估指标
    target_names = ['class 1', 'class 2', 'class 3', 'class 4', 'class 5']
    print(classification_report(five_label, five_pred, target_names=target_names))

    # 计算混淆矩阵
    confusion = confusion_matrix(five_label, five_pred)
    print(confusion)


if __name__ == '__main__':
    # three_class_resave()

    # statistic_three_classfication()
    statistic_five_classfication()